<template>
  <module
    ref="module"
    class="complaint-satisfaction"
    :borderConfig="borderConfig"
    :moduleConfig="moduleConfig"
    :octagon="octagon"
    :barLineConfigTop="barLineConfigTop"
    :gaugeTop="gauge"
    :barLineConfigBottom="barLineConfigBottom"
    :pyramidBottom="pyramid">
  </module>
</template>

<script>
import Module from './../components/Module.vue'
export default {
  name: 'complaintSatisfaction',
  components: {
    Module
  },
  data () {
    return {
      borderConfig: {
        moduleType: '1x1',
        icon: '',
        titleText: '客户满意度',
        goPageUrl: '/home/satisfaction'
      },
      moduleConfig: {
        isOctagon: true, // 是否需要头部的八边形
        topType: '12', // 第一层显示内容
        bottomType: '13' // 第二层显示内容
      },
      barLineConfigTop: {
        title: {
          text: '近一年客服月度满意率'
        },
        axis: {
          yAxisConversion: '%', // Y轴坐标%
          xAxisDataType: 1, // X轴坐标从当前月份开始
          xAxisLabelInterval (index, name) {
            if (index % 2 === 1) {
              return name
            }
          }
        },
        yAxis: {
          max: 100
        },
        tooltipsConfig: {
          symbol: '%',
          tipsColor: ['#b60d81'],
          isNearlyYear: true // 月份是否是当前月份开始
        },
        seriesData: []
      },
      barLineConfigBottom: {
        title: {
          text: '近一年投诉趋势'
        },
        axis: {
          yAxisConversion: 'w', // Y轴坐标%
          xAxisDataType: 1, // X轴坐标从当前月份开始
          xAxisLabelInterval (index, name) {
            if (index % 2 === 1) {
              return name
            }
          }
        },
        yAxis: {},
        tooltipsConfig: {
          tipsColor: ['#2e92cb'],
          isNearlyYear: true // 月份是否是当前月份开始
        },
        seriesData: []
      },
      gauge: {
        title: {
          text: '本月满意率'
        },
        angle: '',
        radius: '82%'
      }, // 本月满意率
      pyramid: {
        title: '本月投诉(件)',
        imgIndex: [0],
        tipsMessage: [{
          beforeMessage: '本月投诉',
          message: '',
          unit: '件'
        }]
      },
      octagon: {
        iconClass: ['icon-tousu', 'icon-guanbitixing'],
        octagonName: ['今日投诉', '待关闭投诉'],
        octagonNumber: [0, 0],
        octagonUnit: ['个', '个']
      },
      complainInformation: {}, // 投诉
      satisfactionInformation: {}, // 满意度
      refreshInterval: 0
    }
  },
  computed: {
    organId () {
      return this.$store.state.organId
    },
    isIndex () {
      return this.$store.state.isIndex
    }
  },
  methods: {
    showTips  (data) {
      this.$parent.showTips(data)
    },
    getData (type, refreshInterval, isRefresh) {
      if (type === 'init') {
        this.orderApi.getSatisfiedRateInfo({ organId: this.organId }).then(res => {
          if (res.data.code === '0') {
            let data = res.data.data
            this.satisfactionInformation = data
            this.gauge.angle = data.mthRate.indexOf('%') > -1 ? data.mthRate.replace('%', '') : data.mthRate
          } else {
            this.satisfactionInformation = {}
            this.gauge.angle = ''
            this.$parent.showTips('请求失败，点击确认继续刷新')
          }
          if (!isRefresh) {
            this.handleData()
          }
        }, res => {
          this.satisfactionInformation = {}
          this.gauge.angle = ''
          this.handleData()
          this.$parent.showTips('请求失败，点击确认继续刷新')
        })
        this.orderApi.getComplainCountInfo({ organId: this.organId }).then(res => {
          if (res.data.code === '0') {
            let data = res.data.data
            this.complainInformation = data
            this.octagon.octagonNumber = [data.dayCount, data.waitCloseCount]
            this.pyramid.tipsMessage[0].message = data.mthCount
          } else {
            this.complainInformation = {}
            this.octagon.octagonNumber = [0, 0]
            this.pyramid.tipsMessage[0].message = 0
            this.$parent.showTips('请求失败，点击确认继续刷新')
          }
          if (!isRefresh) {
            this.handleData2()
          }
        }, res => {
          this.complainInformation = {}
          this.octagon.octagonNumber = [0, 0]
          this.pyramid.tipsMessage[0].message = 0
          this.handleData2()
          this.$parent.showTips('请求失败，点击确认继续刷新')
        })
        if (refreshInterval) {
          this.refreshInterval = refreshInterval
          this.automaticRedrawing()
        }
      } else {
        this.handleData()
        this.handleData2()
      }
    },
    handleData () {
      let data = this.satisfactionInformation
      let seriesData = []
      if (JSON.stringify(data) !== '{}') {
        data.hisMths.forEach(item => {
          seriesData.push(parseFloat(item.hisRate.replace('%', '')).toFixed(2))
        })
      }
      this.barLineConfigTop.seriesData = [{
        data: seriesData,
        name: '满意率',
        colorIndex: 1
      }]
    },
    handleData2 () {
      let bottomSeriesData = []
      let bottomData = this.complainInformation
      if (JSON.stringify(bottomData) !== '{}') {
        bottomData.hisMths.forEach(item => {
          bottomSeriesData.push(parseInt(item.hisCount))
        })
      }
      this.barLineConfigBottom.seriesData = [{
        data: bottomSeriesData,
        name: '投诉量',
        colorIndex: 0,
        isBar: true
      }]
    },
    resizeChart () {
      this.$refs.module.resizeChart()
    },
    // 根据接口返回的时间间隔请求接口
    automaticRedrawing () {
      clearInterval(window.complaintSatisfactionTimer)
      if (this.refreshInterval > 0 && this.isIndex) {
        window.complaintSatisfactionTimer = setInterval(() => {
          this.getData('init', '', true)
        }, this.refreshInterval * 1000)
      }
    }
  },
  mounted () {
  },
  destroyed () {
    clearInterval(window.complaintSatisfactionTimer)
  }
}
</script>
<style lang="scss" scoped>
.customer-satisfaction {
  .module-content {
    width: 100%;
    height: 100%;
  }
  .module-left {
    width: 70%;
    height: 100%;
    float: left;
  }
  .module-right {
    width: 30%;
    height: 65%;
    transform: translate(0, 46%);
    float: left;
    position: relative;
    & /deep/ .singleRing-title {
      position: absolute;
      left: 0;
      bottom: 4%;
      color: #fff;
    }
  }
}
</style>
